package cn.harry.controller.wx;

import cn.harry.common.api.CommonResult;
import cn.harry.common.utils.SysUserUtils;
import cn.harry.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 微信公众号菜单管理
 * 官方文档：https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
 * WxJava开发文档：https://github.com/Wechat-Group/WxJava/wiki/MP_自定义菜单管理
 * @author honghh
 */
@RestController
@RequestMapping("wx/menu")
@RequiredArgsConstructor
@Api(tags = {"公众号菜单-管理后台"})
public class WxMenuManageController {
    private final WxMpService wxService;
    @Autowired
    private WxMpService wxMpService;

    @ApiOperation("list => 获取公众号菜单")
    @PreAuthorize("@ss.hasPermi('wx:menu:list')")
    @GetMapping(value = "/getMenu")
    public CommonResult<WxMpMenu> getMenu(@CookieValue String appid) throws WxErrorException {
        SysUser user = SysUserUtils.getSysUser();

        wxMpService.switchoverTo(appid);
        WxMpMenu wxMpMenu = wxService.getMenuService().menuGet();
        return CommonResult.success(wxMpMenu);
    }

    /**
     * 创建、更新菜单
     */
    @PostMapping("/updateMenu")
    @PreAuthorize("@ss.hasPermi('wx:menu:save')")
    @ApiOperation(value = "创建、更新菜单")
    public CommonResult updateMenu(@CookieValue String appid, @RequestBody WxMenu wxMenu) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        wxService.getMenuService().menuCreate(wxMenu);
        return CommonResult.success();
    }
}
